package com.orangelife.mobile.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.InputFilter;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.EditText;

import com.curry.orangelife.mobile.R;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;

public class SafetyCodeEditText extends EditText {

    private int DEFAULT_CONT_MARGIN;
    private int DEFAULT_SPLIT_LINE_WIDTH;

    private int mBorderColor;
    private float mBorderWidth = 0;
    private float mBorderRadius = 4;
    private float mDotRadius = 8;

    private int mPasswordLength = 3;//格子数
    private int mPasswordColor = 0xFFCCCCCC;
    private float mPasswordWidth = 3;
    private float mPasswordSize = 20;

    private Paint mPasswordPaint = new Paint(ANTI_ALIAS_FLAG);
    private Paint mBorderPaint = new Paint(ANTI_ALIAS_FLAG);
    private int mTextLength;
    private String mInputType;
    private Bitmap mPassWordBitmap;

    public SafetyCodeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        DEFAULT_CONT_MARGIN = (int) getResources().getDimension(R.dimen.divider_dp);
        DEFAULT_SPLIT_LINE_WIDTH = (int) getResources().getDimension(R.dimen.divider);
        mBorderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, dm);
        mBorderRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mBorderRadius, dm);
        mPasswordLength =
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mPasswordLength, dm);
        mPasswordWidth =
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mPasswordWidth, dm);
        mPasswordSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mPasswordSize, dm);
        mBorderColor = getResources().getColor(R.color.darkOrange);

        TypedArray a =
                context.getTheme().obtainStyledAttributes(attrs, R.styleable.SafetyCodeEditText, 0, 0);
        mBorderColor = a.getColor(R.styleable.SafetyCodeEditText_pivBorderColor, mBorderColor);
        mBorderWidth = a.getDimension(R.styleable.SafetyCodeEditText_pivBorderWidth, mBorderWidth);
        mBorderRadius = a.getDimension(R.styleable.SafetyCodeEditText_pivBorderRadius, mBorderRadius);
        mPasswordLength = a.getInt(R.styleable.SafetyCodeEditText_pivPasswordLength, mPasswordLength);
        mPasswordColor = a.getColor(R.styleable.SafetyCodeEditText_pivPasswordColor, mPasswordColor);
        mPasswordWidth =
                a.getDimension(R.styleable.SafetyCodeEditText_pivPasswordWidth, mPasswordWidth);
        mPasswordSize = a.getDimension(R.styleable.SafetyCodeEditText_pivPasswordSize, mPasswordSize);
        mDotRadius = a.getDimension(R.styleable.SafetyCodeEditText_pivDotRadius, mDotRadius);
        mInputType = a.getString(R.styleable.SafetyCodeEditText_inputType);
        if (mInputType == null || mInputType.equals("")) mInputType = "numberPassword";
        a.recycle();

        mBorderPaint.setStrokeWidth(mBorderWidth);
        mBorderPaint.setColor(mBorderColor);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mPasswordPaint.setStrokeWidth(mPasswordWidth);
        mPasswordPaint.setStyle(Paint.Style.FILL);
        mPasswordPaint.setColor(mPasswordColor);
        mPasswordPaint.setTextSize(mPasswordSize);
        //mPassWordBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_password);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();
        // 边框
        mBorderPaint.setStrokeWidth(6);
        RectF rect = new RectF(0, 0, width, height);
        canvas.drawRoundRect(rect, mBorderRadius, mBorderRadius, mBorderPaint);

        // 分割线
        mBorderPaint.setStrokeWidth(3);
        for (int i = 1; i < mPasswordLength; i++) {
            float x = width * i / mPasswordLength;
            canvas.drawLine(x, 0, x, height, mBorderPaint);
        }

        // 输入码
        Paint.FontMetricsInt fm = mPasswordPaint.getFontMetricsInt();

        float cx;
        final float cy = (height - fm.bottom - fm.top) / 2;

        float half = width / mPasswordLength / 2;
        String text = String.valueOf(getText());
        String s;

        if (mInputType.equals("textPassword") || mInputType.equals("numberPassword")) {
            for (int i = 0; i < mTextLength; i++) {
                final float x = width * i / mPasswordLength + half;
                canvas.drawCircle(x, height / 2, mDotRadius, mPasswordPaint);
            }
        } else {
            for (int i = 0; i < mTextLength; i++) {
                s = String.valueOf(text.charAt(i));
                cx = width * i / mPasswordLength + half
                        - mPasswordSize / 4;  //-passwordSize/4为近似的实际水平偏移量的修正,原因不明
                canvas.drawText(s, cx, cy, mPasswordPaint);
            }
        }
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        this.mTextLength = text.toString().length();
        if (mTextLength == mPasswordLength && listener != null) {
            listener.onFinish(text.toString());
        }
        invalidate();
    }

    private OnInputFinishListener listener;

    public void setOnInputFinishListener(OnInputFinishListener listener2) {
        this.listener = listener2;
    }

    public interface OnInputFinishListener {
        void onFinish(String text);
    }

    public int getmBorderColor() {
        return mBorderColor;
    }

    public void setmBorderColor(int mBorderColor) {
        this.mBorderColor = mBorderColor;
        mBorderPaint.setColor(mBorderColor);
        invalidate();
    }

    public float getmBorderWidth() {
        return mBorderWidth;
    }

    public void setmBorderWidth(float borderWidth) {
        this.mBorderWidth = borderWidth;
        mBorderPaint.setStrokeWidth(borderWidth);
        invalidate();
    }

    public float getmBorderRadius() {
        return mBorderRadius;
    }

    public void setmBorderRadius(float mBorderRadius) {
        this.mBorderRadius = mBorderRadius;
        invalidate();
    }

    public int getmPasswordLength() {
        return mPasswordLength;
    }

    public void setmPasswordLength(int mPasswordLength) {
        this.mPasswordLength = mPasswordLength;
        setFilters(new InputFilter[]{new InputFilter.LengthFilter(mPasswordLength)});
        invalidate();
    }

    public int getmPasswordColor() {
        return mPasswordColor;
    }

    public void setmPasswordColor(int mPasswordColor) {
        this.mPasswordColor = mPasswordColor;
        mPasswordPaint.setColor(mPasswordColor);
        invalidate();
    }

    public float getmPasswordWidth() {
        return mPasswordWidth;
    }

    public void setmPasswordWidth(float mPasswordWidth) {
        this.mPasswordWidth = mPasswordWidth;
        mPasswordPaint.setStrokeWidth(mPasswordWidth);
        invalidate();
    }
}
